package com.woniu.order.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class DateUtil {
    /**
     * 比较时间
     * @param ds1 起始时间
     * @param ds2 结束时间
     * @return 返回布尔值
     */
    public static boolean compareDateByDateString(String ds1,String ds2){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

        try {
            Date d1=format.parse(ds1);
            Date d2=format.parse(ds2);
            return d1.getTime()>=d2.getTime();
        } catch (ParseException e) {
            throw new RuntimeException(e);

        }
    }
    /**
     * //字符串转localDate类型时间
     */
    public static LocalDateTime stringConversionLocalDate(String time){
        Date date = stringConversionDate(time);
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant,zoneId);
        return localDateTime;
    }

    /**
     * 字符串转日期
     * @param time
     * @return
     */
    public static Date stringConversionDate(String time){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date;
        try {
            date = simpleDateFormat.parse(time);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return date;
    }

    /**
     * 日期转字符串
     * @param time
     * @return
     */
    public static String dateConversionString(Date time){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = simpleDateFormat.format(time);
        return date;
    }

    /**
     * lang类型转时间字符串
     * @param time
     * @return
     */
    public static String longConversionString(long time){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = simpleDateFormat.format(time);
        return date;
    }

}
